package com.singletonapps.evory.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;



import com.singletonapps.evory.dto.UserRoleDto;

@Entity
@Table(name = "USERS_ROLE")
public class UsersRole implements Serializable {
    
    private static final long serialVersionUID = 4590559136604473760L;

    @Id
    @SequenceGenerator(name = "userRole_seq", sequenceName = "SEQ_USERS_ROLE_ID", allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userRole_seq")
    @Column(name = "ID")
    private Long id;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "ROLE_ID")
    private Role role;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "USER_ID")
    private User user;
    
    @Column(name = "DATE_CREATE")
    private Date dateCreate;
    
    @Column(name = "DATE_UPDATE")
    private Date dateUpdate;
    
    @Version
    private Integer version;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getDateCreate() {
        return dateCreate;
    }

    public void setDateCreate(Date dateCreate) {
        this.dateCreate = new Date(dateCreate.getTime());
    }

    public Date getDateUpdate() {
        return dateUpdate;
    }

    public void setDateUpdate(Date dateUpdate) {
        this.dateUpdate = new Date(dateUpdate.getTime());
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
    
    public UserRoleDto toDto(){
        UserRoleDto dto = new UserRoleDto();
        
        dto.setId(this.id);
        dto.setRole(this.role.toDto());   
        dto.setUser(this.user.toDto());
        dto.setDateCreate(this.dateCreate);
        dto.setDateUpdate(this.dateUpdate);
        dto.setVersion(this.version);
        
        return dto;
    }
    

}
